package com.acme.bankapp.service.bank;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.acme.bankapp.domain.bank.Account;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.CheckingAccount;
import com.acme.bankapp.domain.bank.Client;

public class BankReporter {
	public int getNbOfCustomers(Bank bank) {
		return bank.getClients().size();
	}
	
	public int getNbOfAccounts(Bank bank) {
		return bank.getAllAccounts().size();
	}
	
	public List<Account> getAccountsSortedBySum(Bank bank) {
		List<Account> accList = bank.getAllAccounts();
		Collections.sort(accList, new Comparator<Account>() {
		    public int compare(Account c1, Account c2) {
		        return Double.compare(c1.getBalance(),c2.getBalance()); 
		    }
		});
		return accList;
	}
	
	public double getBankCredit(Bank bank) {
		double credit = 0;
		List<Account> accList = bank.getAllAccounts();
		for (Account acc: accList) {
			if ((acc instanceof CheckingAccount) && (acc.getBalance() < 0)) {
					credit += acc.getBalance();
			}
		}
		return credit;
	}
	
	public Map<Client,Collection<Account>> getCustomerAccount(Bank bank) {
		Map<Client,Collection<Account>> clAccMap = new HashMap<>();
		
		for (Client cl: bank.getClients()) {
			clAccMap.put(cl, cl.getAccounts());
		}
		
		return Collections.unmodifiableMap(clAccMap);
	}
}
